<?php 

class paomeHallController extends base
{
    public function __construct()
    {
        parent::__construct('show');
        $this->in_user();
    }

    /**
    * 根据时间判断赛季所处阶段
    */
    public function dogetPhaseByTime()
    {
        // 获取传入参数
    	$start_time = common::request('start_time', 'G');
    	$end_time = common::request('end_time', 'G');
    	$message = '';
    	if($start_time && $end_time){
    		$start_time = strtotime($start_time);
    		$end_time = strtotime($end_time);
    		//根据时间获取所处赛季
    		$season = $this->load("paome/paomeHall")->getSeasonByTime($start_time, $end_time); 		
    		if(!empty($season)){
    			//根据时间获取所处阶段
	    		$phase1 = $this->load("paome/paomeHall")->getPhaseByTime($start_time, $start_time);
	    		$phase2 = $this->load("paome/paomeHall")->getPhaseByTime($end_time, $end_time);
	    		if(!empty($phase1) && !empty($phase2)){
	    			//所处阶段字典
		    		$type = array(
		    			1 => '第一轮招募期',
		    			5 => '第一轮预选赛',
		    			6 => '第一轮预选赛结束空档期',
		    			10 => '第一轮决赛',
		    			11 => '第一轮决赛结束空档期',
		    			12 => '第一轮休赛期',
		    			15 => '第二轮预选赛',
		    			16 => '第二轮预赛结束空档期',
		    			20 => '第二轮决赛',
		    			21 => '第二轮决赛结束空档期',
		    			22 => '第二轮休赛期',
		    			25 => '第三轮预选赛',
		    			26 => '第三轮预赛结束空档期',
		    			30 => '第三轮决赛',
		    			31 => '第三轮决赛空档期',
		    			32 => '第三轮休赛期',
		    			105 => '争霸赛',
		    			106 => '争霸赛结束空档期'
		    		);
		    		if($phase1['type'] == $phase2['type']){
		    			//同一阶段
		    			$message = "第". $season['type'] ."赛季". $type[$phase1['type']];
		    		}else{
		    			//跨阶段
		    			$message = "第". $season['type'] ."赛季". $type[$phase1['type']] ."至". $type[$phase2['type']];
		    		}
	    		}else{
	    			$message = "该赛季这个时间段暂无轮次信息";
	    		}	    		
    		}else{
    			$message = "暂无赛季信息";
    		} 		
    	}else{
    		$message = '请选择时间';
    	}
    	echo json_encode(array("status" => 'ok', 'message' => $message));
    }

    /**
     * 跑么杯活动(大厅功能管理)--俱乐部推荐
     */
    public function dohallClubRecommend()
    { 	
        $hall_title = common::request('hall_title','R');   //总标题
        $clubId = common::request('clubId','R');  //俱乐部id(第一层添加用)
        $optype = common::request('optype','R');  //操作类型
        $club_id = common::request('club_id','R');  //俱乐部id
        $nick_name = common::request('nick_name','R');  //昵称
        $header = common::request('header','R');  //头像
        $sort = common::request('sort','R');    //排序位置
        $start_time = common::request('start_time','R');   //开始时间
        $end_time = common::request('end_time','R');   //结束时间
        $reason = common::request('reason','R');    //推荐原因

        if($optype == 1){
            if(!empty($clubId)){
                $clubRecommend = array();

                //根据输入的club_id查找俱乐部并显示
                $clubInfo = $this->load('paome/paomeClub')->getRow("id = {$clubId}");
                if($clubInfo && $clubInfo['status'] == 0){
                    $clubRecommend['club_id'] = $clubId;
                    $clubRecommend['nick_name'] = $clubInfo['title'];  //俱乐部名称
                    $clubRecommend['header'] = $clubInfo['image'];
                    $clubRecommend['sort'] = '';
                    $clubRecommend['start_time'] = '';
                    $clubRecommend['end_time'] = '';
                    $clubRecommend['reason'] = '';
                    $this->redis->hset(REDIS_PREFIX.'clubRecommend', $clubId, json_encode($clubRecommend));
                }else{
                    exit("暂无该俱乐部信息或该俱乐部已解散！");
                }
            }else{
                exit("参数不完整！");
            }
        }

        if($optype == 2){
            if($club_id){
                $time = time();
                foreach ($club_id as $key => $id) {
                    if($id===''){
                        continue;
                    }
                    if(!empty($sort[$key])){
                    	$clubInfo = $this->load("paome/paomeClub")->getClubInfo($id);
	                    if(empty($clubInfo) || $clubInfo['status'] == -5){
	                    	exit("id为".$id."的俱乐部已解散，不能推荐");
	                    }
                    }
                    
                    if(empty($start_time[$key]) || empty($end_time[$key])){
                        exit("id为".$id."的俱乐开始时间或结束时间不能为空");
                    }
                    if($start_time[$key] >= $end_time[$key]){
                        exit("id为".$id."的俱乐开始时间必须小于结束时间");
                    }
                    if(!empty($reason[$key]) && mb_strlen($reason[$key], 'utf-8') > 8){
                    	exit("id为".$id."的俱乐推荐原因不能大于8个字");
                    }
                    $clubParams = array();
                    $clubParams['club_id'] = $id;
                    $clubParams['nick_name'] = $nick_name[$key];
                    $clubParams['header'] = $header[$key];
                    $clubParams['sort'] = $sort[$key];
                    $clubParams['reason'] = $reason[$key];
                    $clubParams['start_time'] = strtotime($start_time[$key]);
                    $clubParams['end_time'] = strtotime($end_time[$key]);
                    $clubParams['add_time'] = $time;
                    $clubParams['type'] = 1;

                    //判断该条数据是否存在
                    $club = $this->load('paome/paomeHall')->getRow("club_id = {$id}");
                    if(empty($club)){
                        $this->load('paome/paomeHall')->insert($clubParams);
                    }else{
                        $this->load('paome/paomeHall')->update($clubParams, array('id' => $club['id']));
                    }
                    
                    $this->redis->hset(REDIS_PREFIX.'clubRecommend', $id, json_encode($clubParams));
                    //删除没有排序的缓存数据
                    if(empty($sort[$key])){
                        $this->redis->hdel(REDIS_PREFIX.'clubRecommend', $id);
                    }
                }
            }
            if(!empty($hall_title)){
                $this->redis->set(REDIS_PREFIX . 'clubRecommendTitle', $hall_title);
            }
        }
        //标题缓存
        $hallTitle = $this->redis->get(REDIS_PREFIX . 'clubRecommendTitle');
        $recommendInfo = $this->load('paome/paomeHall')->getclub();
        
        //过期推荐变为历史
        if($recommendInfo){
        	foreach ($recommendInfo as &$v) {
        		$now = date('Y-m-d H:i:s');
        		if(!empty($v['start_time']) && !empty($v['end_time'])){
        			if($v['start_time'] > $now || $v['end_time'] < $now){
	        			$this->redis->hdel(REDIS_PREFIX.'clubRecommend', $v['club_id']);
	                    $club = $this->load('paome/paomeHall')->getRow("club_id = {$v['club_id']}");
	                    $this->load('paome/paomeHall')->update(array('sort'=>0), array('id' => $club['id']));
	        		}
        		}
        	}
        }
        //判断当前所处赛季
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        
        $this->view->assign('season',$season['type']);
        $this->view->assign('recommendInfo',$recommendInfo);
        $this->view->assign('hallTitle',$hallTitle);
        $this->view->display('paome/hallClubRecommend');
    }

    /**
     * 大厅俱乐部推荐历史
     * @return [type] [description]
     */
    public function dohallClubHistory(){
        $p = common::request('p', 'G', 1);   //页码
        
        $isSearch = common::request('s', 'P','');
        if($isSearch == 'search'){
            $p = 1;
        }
        $nick_name = common::request('nick_name', 'R','');

        $where = "type = 1 and sort = 0";
        !empty($nick_name) && $where .= " and nick_name like \"%{$nick_name}%\"";

        $search = compact('nick_name');
        $offset = 20;        
        $num = $this->load('paome/paomeHall')->getOne("count(1)", $where);

        $parameter = "&nick_name=".$nick_name;
        //分页
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset,$parameter);
        $pageStr = $page->show();

        $start = ($p - 1) * $offset;

        $order = "add_time desc ";
        $limit = "$start,$offset";
        $setList = $this->load('paome/paomeHall')->getAll($where, $order, $limit);
        
        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('setList', $setList);
        $this->view->assign('search', $search);
        $this->view->display('paome/hallClubHistory');
    }

    /**
     * 大厅俱乐部推荐历史-再次推荐
     * @return [type] [description]
     */
    public function dohallClubAgain(){
        $club_id = common::request('club_id', 'R');
        $nick_name = common::request('nick_name', 'R');
        $header = common::request('header', 'R');
        $start_time = common::request('start_time', 'R');
        $end_time = common::request('end_time', 'R');
        $reason = common::request('reason', 'R');   
        $cid = common::request('cid', 'R');

        $clubInfo = $this->load("paome/paomeClub")->getClubInfo($club_id);
        if(empty($clubInfo) || $clubInfo['status'] == -5){
        	exit("该俱乐部已解散，不能推荐");
        }
        $recommendAgain = array(
        	'club_id' => $club_id,
        	'nick_name' => $nick_name,
        	'header' => $header,
        	'sort' => $club_id,
        	'start_time' => strtotime($start_time),
        	'end_time' => strtotime($end_time),
        	'reason' => $reason,
        	'add_time' => time(),
        	'type' => 1
        );
        $this->redis->hset(REDIS_PREFIX.'clubRecommend', $club_id, json_encode($recommendAgain));

        $this->load('paome/paomeHall')->update($recommendAgain, array('id' => $cid));
        echo json_encode(array('status' => 'ok'));
    }

    /**
     * 跑么杯活动(大厅功能管理)--个人用户推荐
     */
    public function dohallUserRecommend()
    {
        $hall_title = common::request('hall_title','R');   //总标题
        $userId = common::request('userId','R');  //用户id(第一层添加用)
        $optype = common::request('optype','R');  //操作类型
        $uid = common::request('uid','R');  //用户id
        $nick_name = common::request('nick_name','R');  //昵称
        $header = common::request('header','R');  //头像
        $sort = common::request('sort','R');    //排序位置
        $start_time = common::request('start_time','R');   //开始时间
        $end_time = common::request('end_time','R');   //结束时间
        $reason = common::request('reason','R');    //推荐原因

        if($optype == 1){
            if(!empty($userId)){
                $clubRecommend = array();

                $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $userId);
                if(empty($userInfo)){
                    exit("没有该用户信息");
                }
                //根据输入的userId查看用户是否参赛        
                $isUserJoin = $this->db->getRowTable('r_pm_club_users', array(), "uid = {$userId}");
                
                if($isUserJoin){
                    $userRecommend['user_id'] = $userId;
                    $userRecommend['nick_name'] = $userInfo['nickname'];  
                    $userRecommend['header'] = $userInfo['header'];
                    $userRecommend['sort'] = '';
                    $userRecommend['start_time'] = '';
                    $userRecommend['end_time'] = '';
                    $userRecommend['reason'] = '';
                    $this->redis->hset(REDIS_PREFIX.'userRecommend', $userId, json_encode($userRecommend));
                }else{
                    exit("该用户暂时没有参赛！");
                }
            }else{
                exit("参数不完整！");
            }
        }

        if($optype == 2){
            if($uid){
                $time = time();
                foreach ($uid as $key => $id) {
                    if($key===''){
                        continue;
                    }
                    if(!empty($sort[$key])){
                    	if(empty($start_time[$key]) || empty($end_time[$key])){
	                        exit("id为". $key ."的用户开始时间或结束时间不能为空");
	                    }
	                    if($start_time[$key] >= $end_time[$key]){
	                        exit("id为". $key ."的用户开始时间必须小于结束时间");
	                    }
                    }
                    
                    $userParams = array();
                    $userParams['uid'] = $key;
                    $userParams['nick_name'] = $nick_name[$key];
                    $userParams['header'] = $header[$key];
                    $userParams['sort'] = $sort[$key];
                    $userParams['reason'] = $reason[$key];
                    $userParams['start_time'] = strtotime($start_time[$key]);
                    $userParams['end_time'] = strtotime($end_time[$key]);
                    $userParams['add_time'] = $time;
                    $userParams['type'] = 2;

                    //判断该条数据是否存在
                    $user = $this->load('paome/paomeHall')->getRow("uid = {$key}");
                    if(empty($user)){
                        $this->load('paome/paomeHall')->insert($userParams);
                    }else{
                        $this->load('paome/paomeHall')->update($userParams, array('id' => $user['id']));
                    }
                    
                    $this->redis->hset(REDIS_PREFIX.'userRecommend', $key, json_encode($userParams));
                    //删除没有排序的缓存数据
                    if(empty($sort[$key])){
                        $this->redis->hdel(REDIS_PREFIX.'userRecommend', $key);
                    }
                }
            }
            if(!empty($hall_title)){
                $this->redis->set(REDIS_PREFIX . 'userRecommendTitle', $hall_title);
            }
        }
        //标题缓存
        $hallTitle = $this->redis->get(REDIS_PREFIX . 'userRecommendTitle');
        $recommendInfo = $this->load('paome/paomeHall')->getuser();
        //过期推荐变为历史
        if($recommendInfo){
        	foreach ($recommendInfo as &$v) {
        		$now = date('Y-m-d H:i:s');
        		if(!empty($v['start_time']) && !empty($v['end_time'])){
        			if($v['start_time'] > $now || $v['end_time'] < $now){
	        			$this->redis->hdel(REDIS_PREFIX.'userRecommend', $v['uid']);
	                    $club = $this->load('paome/paomeHall')->getRow("uid = {$v['uid']}");
	                    $this->load('paome/paomeHall')->update(array('sort'=>0), array('id' => $club['id']));
	        		}
        		}
        	}
        }
        //判断当前所处赛季
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        
        $this->view->assign('season',$season['type']);
        $this->view->assign('recommendInfo',$recommendInfo);
        $this->view->assign('hallTitle',$hallTitle);
        $this->view->display('paome/hallUserRecommend');
    }

    /**
     * 大厅个人用户推荐历史
     * @return [type] [description]
     */
    public function dohallUserHistory(){
        $p = common::request('p', 'G', 1);   //页码
        
        $isSearch = common::request('s', 'P','');
        if($isSearch == 'search'){
            $p = 1;
        }
        $nick_name = common::request('nick_name', 'R','');

        $where = "type = 2 and sort = 0";
        !empty($nick_name) && $where .= " and nick_name like \"%{$nick_name}%\"";

        $search = compact('nick_name');
        $offset = 20;        
        $num = $this->load('paome/paomeHall')->getOne("count(1)", $where);

        $parameter = "&nick_name=".$nick_name;
        //分页
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset,$parameter);
        $pageStr = $page->show();

        $start = ($p - 1) * $offset;

        $order = "add_time desc ";
        $limit = "$start,$offset";
        $setList = $this->load('paome/paomeHall')->getAll($where, $order, $limit);
        
        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('setList', $setList);
        $this->view->assign('search', $search);
        $this->view->display('paome/hallUserHistory');
    }

    /**
     * 大厅个人用户推荐历史-再次推荐
     * @return [type] [description]
     */
    public function dohallUserAgain(){
        $uid = common::request('uid', 'R');
        $nick_name = common::request('nick_name', 'R');
        $header = common::request('header', 'R');
        $start_time = common::request('start_time', 'R');
        $end_time = common::request('end_time', 'R');
        $reason = common::request('reason', 'R');   
        $cid = common::request('cid', 'R');
        
        $recommendAgain = array(
        	'uid' => $uid,
        	'nick_name' => $nick_name,
        	'header' => $header,
        	'sort' => $uid,
        	'start_time' => strtotime($start_time),
        	'end_time' => strtotime($end_time),
        	'reason' => $reason,
        	'add_time' => time(),
        	'type' => 2
        );
        $this->redis->hset(REDIS_PREFIX.'userRecommend', $uid, json_encode($recommendAgain));

        $this->load('paome/paomeHall')->update($recommendAgain, array('id' => $cid));
        echo json_encode(array('status' => 'ok'));
    }

    /**
     * 大厅官方咨询
     * @return [type] [description]
     */
    public function dohallAdvisory(){
        $topic_link = common::request('topic_link', 'R');  //热门话题链接
        $advisory = common::request('advisory', 'R');  //咨询名称
        $advisory_link = common::request('advisory_link', 'R');  //咨询链接
        
        if($topic_link && $advisory && $advisory_link){
        	//解析话题链接
        	/*$topicArr = explode('#', $topic_link);
        	$topicName = '#'.$topicArr[1].'#';
        	//获取话题相关信息
        	$newsName = $this->load('paome/paomeHall')->getTopicInfo($topicName);
        	$newsName = preg_replace("/#.+#/is", "", $newsName);
        	$topic = $topicName.' '.$newsName;*/
        	
        	$advisoryInfo = array(
	            //'topic' => $topic,
	            'topic_link' => $topic_link,
	            'advisory' => $advisory,
	            'advisory_link' => $advisory_link
	        );
	        
	        $this->redis->set(REDIS_PREFIX . 'advisoryInfo', json_encode($advisoryInfo));
        }
        
        $historyInfo = $this->redis->get(REDIS_PREFIX . 'advisoryInfo');
        $historyInfo = json_decode($historyInfo, true);
        
        $this->view->assign('historyInfo', $historyInfo);
        $this->view->display('paome/hallAdvisory');
    }

    /**
     * 大厅评论管理
     * @return [type] [description]
     */
    public function dohallComment(){
        $p = common::request('p', 'G', 1);   
        //分页
        $countSql = "select count(1) from r_pm_hall_comment where is_show = 1";
        $num = $this->db->getOne($countSql);

        $offset = 20;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select * from r_pm_hall_comment where is_show = 1 order by add_time desc limit $start,$offset";
        $commentInfo = $this->db->getAll($sql);
        if($commentInfo){
            foreach ($commentInfo as &$v) {
                $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
            }
        }

        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('commentInfo', $commentInfo);
        $this->view->display('paome/hallComment');
    }

    /**
     * 隐藏评论
     * @return [type] [description]
     */
    public function dohideComment(){
        $comment_id = common::request('comment_id', 'P');
        $this->db->update("update r_pm_hall_comment set is_show = 0 where comment_id = {$comment_id}");  
        echo json_encode(array('status' => 'ok')); 
    }

    /**
     * 获取大厅动态数据（推荐话题及最后一条帖子、官方资讯、公告、俱乐部荣誉墙、人个荣誉墙）
     * @return [type] [description]
     */
    public function dogetHallData(){
    	$this->printResult = TRUE;
    	//官方咨询相关
    	$advisoryInfo = $this->redis->get(REDIS_PREFIX . 'advisoryInfo');
    	$advisoryInfo = json_decode($advisoryInfo, true);

    	if(empty($advisoryInfo)){
        	//获取话题相关信息
        	$newsName = $this->load('paome/paomeHall')->getTopicInfo('#抢先玩转跑么杯#');
        	//解析资讯链接
        	$advisory = $this->load('paome/paomeHall')->getNewsName(4710822);
        	$advisoryInfo = array(
	            'topic' => "#抢先玩转跑么杯# ".$newsName,
	            'topic_link' => "joggers://topicSearch/#抢先玩转跑么杯#",
	            'advisory' => $advisory,
	            'advisory_link' => "joggers://news/4710822"
	        );
    	}else{
    		//解析话题链接
        	$topicArr = explode('#', $advisoryInfo['topic_link']);
        	$topicName = '#'.$topicArr[1].'#';
        	//获取话题相关信息
        	$newsName = $this->load('paome/paomeHall')->getTopicInfo($topicName);
        	$newsName = preg_replace("/#.+#/is", "", $newsName);
        	$advisoryInfo['topic'] = $topicName.' '.$newsName;
    	}
    	
    	//俱乐部荣誉墙
    	$clubTitle = $this->redis->get(REDIS_PREFIX . 'clubRecommendTitle');  
        $clubRecommend = $this->load('paome/paomeHall')->getclub(); 
        $now = date('Y-m-d H:i:s', time());       
        foreach ($clubRecommend as $k => $v) {
        	if($v['start_time'] > $now || $v['end_time'] < $now){
        		unset($clubRecommend[$k]);
        	}
        }
        
        //个人用户明星代言人 
        $userTitle = $this->redis->get(REDIS_PREFIX . 'userRecommendTitle');
        $userRecommend = $this->load('paome/paomeHall')->getuser();
        foreach ($userRecommend as $k => $v) {
        	if($v['start_time'] > $now || $v['end_time'] < $now){
        		unset($userRecommend[$k]);
        	}
        }
        
        //名人堂头像
        //正能量榜
        $energyList = $this->load("paome/paomeHall")->getHallEnergyList();
    	//最远记录排行榜(前十名)
    	$maxMileageList = $this->load("paome/paomeHall")->getHallOfFameList('max_mileage');
    	//最受欢迎排行榜(前十名)
    	$sentimentList = $this->load("paome/paomeHall")->getHallOfFameList('support_num');
    	//最快配速排行榜(前十名)
    	$speedtList = $this->load("paome/paomeHall")->getHallOfFameList('speed', 'ASC');
    	$headers = array(
    		'energy' => isset($energyList[0]['header']) ? $energyList[0]['header'] : '',
    		'maxMileage' => isset($maxMileageList[0]['header']) ? $maxMileageList[0]['header'] : '',
    		'sentiment' => isset($sentimentList[0]['header']) ? $sentimentList[0]['header'] : '',
    		'speed' => isset($speedtList[0]['header']) ? $speedtList[0]['header'] : ''
    	);

    	//微信分享链接
    	$uid = $this->uid ? $this->uid : 0;
    	$wxUrl = "http://".$this->config['host']."/paome_webIndex/paoshare";
    	$shareTitle = "我正在参与跑么杯争霸赛，快来加入我的战队赢取5000元奖励吧！";
        $description = "11月14日，就在虎扑跑步";
    	$clubInfo = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid);
        if(!empty($clubInfo)){
        	//$wxUrl = $this->load('paome/share')->pubOpenIdUrl($uid);
        	$wxUrl = "http://".$this->config['host']."/paome_share/shareLink?target_uid=".$uid;
        	$shareTitle = "我正在参与跑么杯争霸赛，需要你的助力！助力即可抽奖！";
            $description = "每人每天可抽奖三次，最高可赢得iPhone7";
        }

        $this->result['result']['wxUrl'] = $wxUrl;
        $this->result['result']['shareTitle'] = $shareTitle;
        $this->result['result']['description'] = $description;
    	$this->result['result']['headers'] = $headers;
        $this->result['result']['advisoryInfo'] = $advisoryInfo;
    	$this->result['result']['clubTitle'] = $clubTitle;
    	$this->result['result']['clubRecommend'] = array_values($clubRecommend);
    	$this->result['result']['userTitle'] = $userTitle;
    	$this->result['result']['userRecommend'] = array_values($userRecommend);
    }

    /**
     * 大厅名人堂
     * @return [type] [description]
     */
    public function dohallOfFame(){
    	$this->printResult = TRUE;
    	//正能量土豪榜(前十名)
    	$energyList = $this->load("paome/paomeHall")->getHallEnergyList();
    	//最远记录排行榜(前十名)
    	$maxMileageList = $this->load("paome/paomeHall")->getHallOfFameList('max_mileage');
    	//最受欢迎排行榜(前十名)
    	$sentimentList = $this->load("paome/paomeHall")->getHallOfFameList('support_num');
    	//最快配速排行榜(前十名)
    	$speedtList = $this->load("paome/paomeHall")->getHallOfFameList('speed', 'ASC');
    	
        $this->result['result']['energyList'] = $energyList;
    	$this->result['result']['maxMileageList'] = $maxMileageList;
    	$this->result['result']['sentimentList'] = $sentimentList;
    	$this->result['result']['speedtList'] = $speedtList; 
    }

    /**
     * 大厅顶部--当前状态及人数
     * @return [type] [description]
     */
    public function dohallStatus(){
    	$this->printResult = TRUE;
    	//获取当前所处阶段及对应时间范围
        $timeRang = $this->load("paome/home")->getTimeRang();
    	if($timeRang){
    		if(date('Y', $timeRang['start_time']) == date('Y', $timeRang['end_time'])){
    			$timeRang['start_time'] = date('m月d日 H:i', $timeRang['start_time']);
	            $timeRang['end_time'] = date('m月d日 H:i', $timeRang['end_time']);
    		}else{
    			$timeRang['start_time'] = date('Y年m月d日 H:i', $timeRang['start_time']);
	            $timeRang['end_time'] = date('Y年m月d日 H:i', $timeRang['end_time']);
    		}
    	}

    	//获取参与人数
    	$userCount = $this->db->getOne("SELECT count(1) FROM r_pm_club_users");
    	$userCount = $userCount * 15;

    	//获取讨论人数
    	$discussCount = $this->db->getOne("SELECT COUNT(1) FROM r_pm_hall_comment");
    	$discussCount = $discussCount * 15;

    	$this->result['result']['timeRang'] = $timeRang;
    	$this->result['result']['userCount'] = $userCount;
    	$this->result['result']['discussCount'] = $discussCount;
    }

    /**
     * 大厅--点击用户头像获取用户信息
     * @return [type] [description]
     */
    public function dogetUserPaome(){
    	$this->printResult = TRUE;
    	$selfUid = $this->uid;   //自己的id(操作者)
    	if(!$selfUid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $target_uid = common::request('target_uid', 'G','');  //点击的用户id(被操作者)
        if(empty($target_uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $target_uid);
        if(empty($userInfo)){
        	$_ENV['error']->getErrors(309);
            exit;
        }

        //是否是自己
        $isSelf = 0;
        if($selfUid == $target_uid){
        	$isSelf = 1;
        }
        
        $member = array('nickname' => $userInfo['nickname'], 'header' => $userInfo['header']);
        $userData = $this->load('paome/center')->getUserData($target_uid, null);
        //获取俱乐部
        $clubInfo = $this->load("paome/paomeHall")->getClubByUid($target_uid);
        if($clubInfo){
        	$clubInfo['isleader'] = 0;
        	if($clubInfo['head_uid'] == $target_uid){
        		$clubInfo['isleader'] = 1;
        	}
        }

        //获取全员跑量榜排名
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        $rankInfo = '--';
        if(!empty($season)){
        	$roundId = $this->load('paome/paomeHall')->getCurrentRound();
        	$rankUserKeyMile = 'rankUser-mileage-'.$season['id'].'-'.$roundId;
	        $rankNum = $this->redis->zRevRank($rankUserKeyMile,$target_uid) !==false ? ($this->redis->zRevRank($rankUserKeyMile,$target_uid)+1) :'';
        	//$rankTotal = $this->load("paome/center")->getUserCount($season);
        	//排名信息
	        if($rankNum/* && $rankTotal*/){
	        	$rankInfo = $rankNum/* .'/'. $rankTotal*/;
	        }
        }
        
        !empty($userData) && $userData['rankInfo'] = $rankInfo;
        empty($userData) && $userData = array('uid'=>'', 'club_id'=>'', 'max_mileage'=>'','mileage'=>'','agree_num'=>'','energy_num'=>'','rankInfo'=>'');

    	//查看是否点赞
    	$isLight = 0;
    	//查看今日是否点赞
        $date = date('Ymd', time());
        $support = $this->db->getRowTable('r_pm_supporters', array(), "type = 2 and is_share = 0 and uid = {$selfUid} and target_uid = {$target_uid} and date = {$date}");
        if(!empty($support)){
            $isLight = 1;
        }
        //查看今日是否打赏
        $isSupport = 0;
        $date = date('Ymd', time());
        $support = $this->db->getRowTable('r_pm_supporters', array(), "type = 1 and is_share = 0 and uid = {$selfUid} and target_uid = {$target_uid} and date = {$date}");
        !empty($support) && $isSupport = 1;

        //获取我的正能量值
        $selfInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $selfUid);
        $total_energy = '';
        if(!empty($selfInfo)){
        	$total_energy = $selfInfo['total_energy'];
        }

        $this->result['result']['userData'] = $userData;
        $this->result['result']['member'] = $member;
    	$this->result['result']['clubInfo'] = $clubInfo;
    	$this->result['result']['isSelf'] = $isSelf;
    	$this->result['result']['isLight'] = $isLight;
    	$this->result['result']['isSupport'] = $isSupport;
    	$this->result['result']['total_energy'] = $total_energy;
    }

    /**
     * 大厅--点赞
     * @return [type] [description]
     */
    public function dopaomeThumbUp(){
    	$this->printResult = TRUE;
    	$selfUid = $this->uid;   //自己的id(操作者)
    	if(!$selfUid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $target_uid = common::request('target_uid','R');  //点击的用户id(被操作者)
        if(empty($target_uid) || $selfUid == $target_uid)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }

        //查看今日是否点赞
        $date = date('Ymd');
        /*$support = $this->db->getRowTable('r_pm_supporters', array(), "type = 2 and is_share = 0 and uid = {$selfUid} and target_uid = {$target_uid} and date = {$date}");*/
        $lightTimes = $this->redis->get(REDIS_PREFIX . "selfUid_" . $selfUid . "_target_uid_" . $target_uid . "_date_" . $date);
        $lightTimes = $lightTimes ? $lightTimes : 0;
        if($lightTimes == 1){
            $_ENV['error']->getErrors(703);
            exit;
        }
        $this->redis->set(REDIS_PREFIX . "selfUid_" . $selfUid . "_target_uid_" . $target_uid . "_date_" . $date, 1);
        $this->redis->expire(REDIS_PREFIX . "selfUid_" . $selfUid . "_target_uid_" . $target_uid . "_date_" . $date, 86400);
        //点赞表更新数据
        //$_ENV['client']->thumbsUserUp($selfUid, $target_uid);

        //获取当前所处赛季
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        //获取当前所处轮次
        $round = $this->load('paome/paomeHall')->getCurrentRound();
        if(!empty($season) && $season['status'] == 1){
        	$round = 1;
        }
        $isInsert = 0;
        $light = 10;
        if($season && $round){
        	//$light = mt_rand(1, 30);   //打赏的人气
        	//查询所属战队人气排名
        	/*$youClubInfo = $this->load('paome/paomeClub')->getUserCurrentClubInfo($target_uid);
        	if($youClubInfo){
        		$rankClubKeySupportNum = 'rankCLub-supportNum-'.$season['id'].'-'.$round;
        		$rankNum = ($this->redis->zRevRank($rankClubKeySupportNum,$youClubInfo['id']) !==false) ? ($this->redis->zRevRank($rankClubKeySupportNum,$youClubInfo['id'])+2) :'';
	        	if($rankNum && $rankNum < 30) {
                    $light = mt_rand(1, $rankNum);
                }
        	}*/
        	
        	$param = array(
	        	'contest_id' => $season['id'], 
	        	'uid' => $selfUid, 
	        	'target_uid' => $target_uid, 
	        	'round' => $round, 
	        	'type' => 2, 
	        	'light' => $light,
	        	'date' => date('Ymd') 
	        );
	        //跑么杯人气表更新
	        $isInsert = $this->db->insertRowTable('r_pm_supporters', $param);
	        $this->load('paome/cup')->addSupportNum($target_uid, $light, 2);
        }
        $this->result['result']['light'] = $light;
    }

    /**
     * 大厅--打赏
     * @return [type] [description]
     */
    public function doreward(){
    	$this->printResult = TRUE;
    	$target_uid = common::request('target_uid','R');
        $energy = abs((int)common::request('energy','R'));
    	$selfUid = $this->uid;   //自己的id(操作者)

    	if(!$target_uid || !$energy || $target_uid == $selfUid){
            $_ENV['error']->getErrors(101);
            exit;
        }
    	if(!$selfUid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!in_array($energy, array(10, 15, 20, 25, 30))){
        	$_ENV['error']->getErrors(6029);
            exit;
        }

        //获取我的正能量值
        $selfInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $selfUid);
        $total_energy = $selfInfo['total_energy'];
        if($total_energy < $energy){
            $_ENV['error']->getErrors(4002);
            exit;
        }

        //查看今日是否打赏
        $date = date('Ymd', time());
        $support = $this->db->getRowTable('r_pm_supporters', array(), "type = 1 and uid = {$selfUid} and target_uid = {$target_uid} and date = {$date}");
        if(!empty($support)){
            $_ENV['error']->getErrors(4001);
            exit;
        }

        //正能量表更新
        $this->load('client')->userEnergyChange($selfUid,'paomePay',[
            'target_uid' => $target_uid,
            'energy_add' => -$energy
        ]);
        
        $this->load('client')->userEnergyChange('175701','paomePaid',[
            'target_uid'=>$selfUid,
            'energy_add'=>$energy
        ]);

        //获取当前所处赛季
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        //获取当前所处轮次
        $round = $this->load('paome/paomeHall')->getCurrentRound();
        if(!empty($season) && $season['status'] == 1){
        	$round = 1;
        }
        $isInsert = 0;
        if($season && $round){
        	$param = array(
	        	'contest_id' => $season['id'], 
	        	'uid' => $selfUid,
	        	'target_uid' => $target_uid, 
	        	'round' => $round, 
	        	'type' => 1, 
	        	'energy' => $energy,
	        	'date' => date('Ymd') 
	        );
	        //跑么杯人气表更新
	        $isInsert = $this->db->insertRowTable('r_pm_supporters', $param);
	        $this->load('paome/cup')->addSupportNum($target_uid, $energy, 1);
        }
        

        //添加社区未读消息
        /*$this->load('bbs')->addUnReadMsg($target_uid, array('msgType'=>5, 'sponsor'=>array('uid'=>$selfUid,'comment_id'=>'0'), 'quote'=>array('news_id'=>null,'comment_id'=>'0')));
        //发送IM消息
        $this->load('message')->insertMessage("2","S10002",$selfUid,$target_uid,"N10",'');*/ 

        $this->result['result']['isInsert'] = $isInsert;
    }

    /**
     * 大厅--讨论区列表
     * @return [type] [description]
     */
    public function dopaomeComment(){
    	$this->printResult = TRUE;
    	$uid = $this->uid ? $this->uid : 0;   
    	$p = common::request('p', 'R', 1);   

        //$countSql = "select count(1) from r_pm_hall_comment where is_show = 1";
        $offset = 20;
        /*$num = $this->db->getOne($countSql);

        //分页
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset);
        $pageStr = $page->show();*/
        $start = ($p - 1) * $offset;

        $list = $this->db->getAll("select * from r_pm_hall_comment where is_show = 1 order by add_time");    // limit $start,$offset
        if($list){
        	foreach ($list as &$v) {
        		$userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $v['uid']);
        		$v['nickname'] = '';
	        	$v['header'] = '';
	        	if($userInfo){
	        		$v['nickname'] = $userInfo['nickname'];
		        	$v['header'] = $userInfo['header'];
	        	}
        		$v['add_time'] = date('m月d日', $v['add_time']);
        		$v['isSelf'] = 0;
        		if($v['uid'] == $uid){
        			$v['isSelf'] = 1;
        		}
        	}
        }
        $this->result['result']['list'] = $list;
    }

    /**
     * 大厅--讨论区(新增)
     * @return [type] [description]
     */
    public function doaddComment(){
    	$this->printResult = TRUE;
    	$uid = $this->uid;   
    	$comment = common::request('comment','R');
    	if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($comment)){
        	$_ENV['error']->getErrors(101);
            exit;
        }
        $time = time();
        $param = array(
        	'uid' => $uid,
        	'comment' => $comment,
        	'add_time' => $time
        );

        //获取用户信息
        $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $uid);
        if(!$userInfo){
        	$_ENV['error']->getErrors(309);
            exit;
        }
        //新增
        $isInsert = $this->db->insertRowTable('r_pm_hall_comment', $param);

        $backInfo = array(
        	'uid' => $uid,
        	'nickname' => $userInfo['nickname'],
        	'header' => $userInfo['header'],
        	'add_time' => date('m月d日', $time),
        	'isSelf' => 1
        );

        $this->result['result']['backInfo'] = $backInfo;
    }
}